package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.myapplication.adpter.VideoAdapter;
import com.example.myapplication.baseMVVM.BaseActivity;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.entiy.VideoEntity;
import com.example.myapplication.main.MainViewModel;
import com.example.myapplication.utils.CheckLogin;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;
    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        videoAdapter = new VideoAdapter(videos);
        binding.rv.setAdapter(videoAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.rv.setLayoutManager(linearLayoutManager);
        viewModel.video(1,30);

        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(binding.rv);

        binding.rv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int position = linearLayoutManager.findFirstVisibleItemPosition();
                StandardGSYVideoPlayer gsyVideoPlayer = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(position, R.id.gsy);
                gsyVideoPlayer.startPlayLogic();
            }
        });
        //添加点赞功能，添加面向切面实现登录判断
        videoAdapter.addChildClickViewIds(R.id.iv_dianzan,R.id.iv_pinglun);
        videoAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()){
                    case R.id.iv_dianzan:
                        dianzan(position);
                        break;
                    case R.id.iv_pinglun:
                        binding.rv.setFlag(false);
                        pinglun(position);
                        break;
                    /*case R.id.iv_close:
                        binding.rv.setFlag(true);
                        break;*/
                }
            }
        });

    }

    private void pinglun(int position) {
    }
    @CheckLogin
    private void dianzan(int position) {
        //已登录，直接动画效果
        ImageView ivDianzan = (ImageView) videoAdapter.getViewByPosition(position,R.id.iv_dianzan);
        /*ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivDianzan, "scaleX", 1, 2);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivDianzan, "scaleY", 1, 2);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(ivDianzan, "alpha", 1, 0, 0.5f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(2000);
        set.play(scaleX).with(scaleY).with(alpha);
        set.start();*/
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.in);
        ivDianzan.startAnimation(animation);
    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                if(entity.getCode()==200){
                    videos.clear();
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected Class<MainViewModel> getViewModel() {
        return MainViewModel.class;
    }
}